#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
#include <QtMath>
#include <QTimeZone>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    if (ui) {
        delete ui;
        ui = nullptr;
    }
}

void Widget::on_pushButtonGetDateTime_clicked()
{
    QDateTime curDateTime = QDateTime::currentDateTime();
    QTimeZone curTimeZone = curDateTime.timeZone();

    //ui->labelTimeZone->setText(curTimeZone.displayName(QTimeZone::StandardTime, QTimeZone::OffsetName));
    ui->labelTimeZone->setText(curDateTime.toTimeZone(QTimeZone(curTimeZone.id())).toString(Qt::ISODate));

    ui->timeEdit->setTime(curDateTime.time());
    ui->lineEditTime->setText(curDateTime.toString("hh:mm:ss"));

    ui->dateEdit->setDate(curDateTime.date());
    ui->lineEditDate->setText(curDateTime.toString("yyyy-MM-dd"));

    ui->dateTimeEdit->setDateTime(curDateTime);
    ui->lineEditDateTime->setText(curDateTime.toString("yyyy-MM-dd hh:mm:ss"));
}

void Widget::on_pushButtonDebugTime_clicked()
{
    QTime TM1(11, 23, 5);
    QString str1 = TM1.toString("HH:mm:ss");
    qDebug("Original time = %s", str1.toLocal8Bit().constData());

    QTime TM2 = TM1.addSecs(150);
    QString str2 = TM2.toString("HH:mm:ss");
    qDebug("150s later time = %s", str2.toLocal8Bit().constData());

    QTime TM3 = QTime::currentTime();
    QString str3 = TM3.toString("HH:mm:ss zzz");
    qDebug("Current time = %s", str3.toLocal8Bit().constData());
    qDebug("hour = %d", TM3.hour());
    qDebug("minute = %d", TM3.minute());
    qDebug("second = %d", TM3.second());
    qDebug("msec = %d", TM3.msec());
}

void Widget::on_pushButtonSetTime_clicked()
{
    QString str = ui->lineEditTime->text();
    str = str.trimmed();
    if ( !str.isEmpty() ) {
        QTime tm = QTime::fromString(str, "hh:mm:ss");
        ui->timeEdit->setTime(tm);
    }
}

void Widget::on_pushButtonDebugDate_clicked()
{
    QDate DT1(2023, 5, 13);
    QString str1 = DT1.toString("yyyy-MM-dd");
    qDebug("DT1 = %s", str1.toLocal8Bit().constData());

    QDate DT2;
    DT2.setDate(2023, 8, 25);
    QString str2 = DT2.toString("yyyy-MM-dd");
    qDebug("DT2 = %s", str2.toLocal8Bit().constData());
    qDebug("Days between DT2 and DT1 = %lld", DT2.daysTo(DT1));

    QDate DT3 = QDate::currentDate();
    QString str3 = DT3.toString("yyyy-MM-dd");
    qDebug("Current date = %s", str3.toLocal8Bit().constData());
    qDebug("year = %d", DT3.year());
    qDebug("month = %d", DT3.month());
    qDebug("day = %d", DT3.day());
    qDebug("Day of week = %d", DT3.dayOfWeek());
}

void Widget::on_pushButtonSetDate_clicked()
{
    QString str = ui->lineEditDate->text();
    str = str.trimmed();
    if ( !str.isEmpty() ) {
        QDate dt = QDate::fromString(str, "yyyy-MM-dd");
        ui->dateEdit->setDate(dt);
    }
}

void Widget::on_pushButtonDebugDateTime_clicked()
{
    QDateTime DT1 = QDateTime::currentDateTime();
    QString str1 = DT1.toString("yyyy-MM-dd hh:mm:ss");
    qDebug("DT1 = %s", str1.toLocal8Bit().constData());

    QDate dt1 = DT1.date();
    QString str2 = dt1.toString("yyyy-MM-dd");
    qDebug("DT1.date() = %s", str2.toLocal8Bit().constData());

    QTime tm1 = DT1.time();
    QString str3 = tm1.toString("hh:mm:ss zzz");
    qDebug("DT1.time() = %s", str3.toLocal8Bit().constData());

    qint64 MS = DT1.toSecsSinceEpoch();
    qDebug("DT1.toSecsSinceEpoch() = %lld", MS);

    MS += 112;
    DT1.setSecsSinceEpoch(MS);
    str3 = DT1.toString("yyyy-MM-dd hh:mm:ss");
    qDebug("DT1 + 112s = %s", str3.toLocal8Bit().constData());
}

void Widget::on_pushButtonSetDateTime_clicked()
{
    QString str = ui->lineEditDateTime->text();
    str = str.trimmed();
    if ( !str.isEmpty() ) {
        QDateTime datetime = QDateTime::fromString(str, "yyyy-MM-dd hh:mm:ss");
        ui->dateTimeEdit->setDateTime(datetime);
    }
}

void Widget::on_calendarWidget_clicked()
{
    QDate dt = ui->calendarWidget->selectedDate();
    QString str = dt.toString("yyyy年M月d日");
    ui->lineEditCalendar->setText(str);
}

